<?php
	class Order extends MY_Controller{	
		function Order() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();
			$_SESSION['redirect'] = $this->uri->uri_string();
		}

		function index(){
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Đơn hàng";
			$data['table'] = 'order';
			$data['controller'] = 'order';
			$data['content_admin'] = 'back_end/order/index';
			$data['sort'] = 'ASC';
		
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			$per_page = 6;
			$uri = 4;
			$data['admin_order'] = $this->mod_lib->load_all('order','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url('back_end/order/index/');
			$config['total_rows']   =  $this->db->count_all('order');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);
			
			$this->load->view('back_end/template_backend',$data);
		} 
		
		function sort($field,$sort = ''){
			$per_page = 5;
			$uri = 4;
			$data['title'] = "Sắp xếp - sản phẩm";
			$data['table'] = 'order';
			$data['controller'] = 'order';
			$data['content_admin'] = 'back_end/order/index';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('order','',array(),array(),array("id"=>"desc"));
			$data['admin_order'] = $this->mod_lib->load_search('order','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
			$config['base_url']     =  site_url('back_end/order/index');

			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);
		}
		
		function search(){
			$data['vl_sort'] = "DESC";	
			$data['sort'] = 'ASC';
			$data['table'] = 'order';
			$check_search = $this->input->POST('formSearch');
			$per_page = 6;
			$data['title'] = "Tìm kiếm - Sản phẩm";
			$data['controller'] = 'order';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/order/index';
				
			$where = array();
			
			if(!empty($check_search)){
				$_SESSION['like'] = array('name_user' =>$this->input->POST('nameSearch'));																												
			}
			
			$data_search = $this->mod_lib->search('order','',$_SESSION['like'],$where,array("id"=>"desc"));
			$data['admin_order'] = $this->mod_lib->load_search('order','',$_SESSION['like'],$where,$per_page,$this->uri->segment(4),array("id"=>"desc"));		
			$config['base_url']     =  site_url('back_end/order/search/');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  4; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function view($id){
			$data['title'] = "Chi tiết đơn hàng";
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/order/view';
			
			//thong tin don hang
			$data['value_order'] = $this->db->get_where('order',array('id'=> $id))->row_array();
			
			//thong tin khach hang
			$data['value_user'] = $this->db->get_where('user',array('id'=> $data['value_order']['id_user']))->row_array();
			
			//thong tin san pham da mua 
			$sql =  "SELECT product.name, order_detail.quantity, product.cost 
					 FROM   product, order_detail
				 	 WHERE  product.id = order_detail.product_id
				 	 AND    order_detail.order_id = $id
				 	";
			$data['value_product'] = $this->db->query($sql)->result_array();
			
			$this->load->view('back_end/template_backend',$data);	
		}

		function deleteId($id) {
			if(!empty($id)){
				
				$this->db->delete('order_detail', array('order_id' => $id));
						
				if($this->db->delete('order', array('id' => $id))){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} 
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			
			redirect('back_end/order');
		}
	
}	
?>